package GPool

import "sync"

type Tasker interface {
	Do()
}

type WorkPool struct {
	work chan Tasker
	wg   sync.WaitGroup
}

func NewWorkPool(cap int) *WorkPool {
	p := &WorkPool{
		work: make(chan Tasker, cap),
	}
	p.wg.Add(cap)
	for i := 0; i < cap; i++ {
		go func() {
			for w := range p.work {
				w.Do()
			}
			p.wg.Done()
		}()
	}
	return p
}

func (p *WorkPool) Run(t Tasker) {
	p.work <- t
}

func (p *WorkPool) Close() {
	close(p.work)
	p.wg.Wait()
}
